home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK2.toast / What's New / • What was new 11⁄99 / Sample Code / Overview / Optimization TN Demos / CBuffFileStream / CreateRandExpNumbers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-13  |  2.8 KB  |  111 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        CreateRandExpNumbers.c
  3.  
  4.     Contains:    Random number utilities.
  5.  
  6.     Written by:    Steve Bollinger
  7.  
  8.     Copyright:    Copyright (c) 1999 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18. */
  19.  
  20. #include <DateTimeUtils.h>
  21. #include <LowMem.h>
  22.  
  23. #include "CreateRandExpNumbers.h"
  24.  
  25. static unsigned GetRandom15Bit(void);
  26. static unsigned long GetRandom32Bit(void);
  27.  
  28. void RandomlySeedRandom(void)
  29. {
  30.     unsigned long        secs;
  31.  
  32.     GetDateTime(&secs);
  33.     LMSetRndSeed(secs);
  34. }
  35.  
  36. void CreateRandomExpNumbers(unsigned long *towhere, unsigned long maxval, unsigned long numnumbers)
  37. {
  38. // Here's the story, we generate random numbers by concatenating series of random bits
  39. // of the length of the magnitude of the maxval and throwing out all values larger than
  40. // the max.
  41.     static unsigned long    exponentValues[32] =
  42.     { 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff, 0x7ff, 0xfff,
  43.       0x1fff, 0x3fff, 0x7fff, 0xffff, 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff,
  44.       0x3fffff, 0x7fffff, 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff,
  45.       0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff };
  46.  
  47.     unsigned                maxmagnitude = 0;
  48.     unsigned long            curnumber;
  49.     unsigned                expmagnitude = 0;
  50.  
  51. // First, find the magnitude of maxval
  52.  
  53.     while (maxval > exponentValues[maxmagnitude])
  54.     {
  55.         maxmagnitude++;
  56.     }
  57.  
  58. // Now, find the magnitude of the magnitude
  59.     while (maxmagnitude > exponentValues[expmagnitude])
  60.     {
  61.         expmagnitude++;
  62.     }
  63.  
  64. // Now, we need to generate exponents and values
  65.     for (curnumber = 0; curnumber < numnumbers; curnumber++)
  66.     {
  67.         unsigned            randmagnitude;
  68.         unsigned long        randval;
  69.  
  70.     // First, calculate random magnitude
  71.         do
  72.         {
  73.             randmagnitude = GetRandom15Bit() & exponentValues[expmagnitude];
  74.         } while (randmagnitude > maxmagnitude);
  75.  
  76.     // Now create a random 32 bit number and mask it down.
  77.         do
  78.         {
  79.             randval = GetRandom32Bit() & exponentValues[randmagnitude];
  80.         } while (randval > maxval);
  81.  
  82.     // Whew, we got one number! Store it and move on.
  83.         towhere[curnumber] = randval;
  84.     }
  85. }
  86.  
  87. unsigned GetRandom15Bit(void)
  88. {
  89.     short            val;
  90.  
  91.     do
  92.     {
  93.         val = Random();
  94.  
  95.         if (0 == val)
  96.             val = -2;            // 0 sux, make it out of range
  97.         if (-1 == val)
  98.             val = 0;            // but -1 is okay.
  99.  
  100.     } while (val < 0 || val > 32767);
  101.  
  102.     return (unsigned) val;
  103. }
  104.  
  105. unsigned long GetRandom32Bit(void)
  106. {
  107.     return (GetRandom15Bit() << 17) |
  108.            (GetRandom15Bit() << 2) |
  109.            (GetRandom15Bit() & 0x3);
  110. }
  111.